/**
 * Project  : monkey
 * Author   : Wu Tian Qiang
 * Date     : 2016/10/18 20:49:30
 */
Ext.define('Monkey.view.user.GrantRolePermissionController', {
    extend: 'Monkey.view.BaseController',
    alias: 'controller.ctrl-grantrolepermission',

    saveRole: function (cmp, newValue, oldValue) {
        var me = this;
        var view = me.getView();
        var method = '';
        var roleIds;
        if (newValue.length > oldValue.length) {
            method = 'POST';
            roleIds = Ext.Array.difference(newValue, oldValue);
        } else {
            method = 'PUT';
            roleIds = Ext.Array.difference(oldValue, newValue)
        }
        if (Ext.isEmpty(roleIds)) {
            return;
        }
        me.request(Monkey.URL.SERVER_URL + '/m/role/user.json' + '?userId=' + view.userId, method, {
            roleIds: Monkey.Utils.arrayToString(roleIds, ',')
        }, function (result) {
            if (!result.success) {
                Ext.Msg.show({
                    title: '提示',
                    message: result.message,
                    buttons: Ext.Msg.OK,
                    icon: Ext.Msg.ERROR
                });
            }
        });
    },

    setAlreadyHaveRole: function (cmp) {
        var me = this,
            view = me.getView();
        me.request(Monkey.URL.SERVER_URL + '/m/user/role.json', 'GET', {
            userId: view.userId,
            fields: 'id,role[id:name]'
        }, function (result) {
            if (result.success) {
                var values = [];
                for (var i = 0; i < result.data.length; i++) {
                    values.push(result.data[i].role.id);
                }
                cmp.setValue(values);
            }
        });
    }


});
